package com.vviton.zyhj.admin.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.vviton.zyhj.common.common.GlobalConstants;
import com.vviton.zyhj.common.content.SpringContext;
import com.wdcloud.login.client.session.SimpleSessionUserHolder;

/**
 * ClassName: UserSessionFilter <br/>
 * Function: 拦截器配置. <br/>
 * Date: 2016年11月10日 下午4:07:31 <br/>
 *
 * @author weiming.chen
 * @version 1.0
 * @since JDK 1.7
 */
public class UserSessionFilter implements Filter {

    /** 日志记录 */
    private Logger logger = Logger.getLogger(UserSessionFilter.class);

    /** 标识 */
    private boolean startFlag = true;

    public UserSessionFilter() {

    }

    /** 销毁 */
    @Override
    public void destroy() {

    }

    /**
     * doFilter,拦截器过滤. <br/>
     * Author: wanrj <br/>
     * Create Date: 2015年9月17日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     *
     * @param req ServletRequest
     * @param resp ServletResponse
     * @param chain FilterChain
     * @throws IOException 异常
     * @throws ServletException 异常
     * @since JDK 1.7
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String path = request.getPathInfo();
        if (startFlag) {
            boolean isExclude = isURLExcept(path);
            if (isExclude) {
                chain.doFilter(request, response);
            } else {
                logger.info("获取平台和消费者账户");
                SimpleSessionUserHolder userHolder = SpringContext.getBean(SimpleSessionUserHolder.class);
                String adminZh = userHolder.getLoginId(request);
                logger.info("平台账户为:" + adminZh);
                if (StringUtils.isEmpty(adminZh)) {
                    response.getWriter().write(GlobalConstants.SESSION_MISS_CODE_AM);
                } else {
                    chain.doFilter(request, response);
                }
            }
        } else {
            chain.doFilter(request, response);
        }
    }

    /**
     * init,初始化. <br/>
     * Author: wanrj <br/>
     * Create Date: 2016年2月1日 <br/>
     * =============================================================== <br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * =============================================================== <br/>
     *
     * @param fConfig FilterConfig
     * @throws ServletException 异常
     * @since JDK 1.7
     */
    @Override
    public void init(FilterConfig fConfig) throws ServletException {
        String startFlag = fConfig.getInitParameter("startFlag");
        this.startFlag = Boolean.parseBoolean(startFlag);
    }

    /**
     * isURLExcept,判断地址是否需要排除. <br/>
     * Author: wanrj <br/>
     * Create Date: 2016年2月1日 <br/>
     * ===============================================================<br/>
     * Modifier: huangqq <br/>
     * Modify Date: 2016年2月1日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param url 路径
     * @return boolean
     * @since JDK 1.7
     */
    private boolean isURLExcept(String url) {
        if (url != null && GlobalConstants.EXCLUDE_SESSION_URL.contains(url)) {
            return true;
        }
        return false;
    }

}